(ns hiccup_plus.core)

(def non-empty? #(not (empty? %)))
(defn merge-maps-with-flexible-string-concat
  "将两个mao进行合并，指定的key值不覆盖而是相加"
  [m1 m2 keys-to-concat]
  (reduce (fn [acc k]
            (let [v1 (get m1 k "")
                  v2 (get m2 k "")
                  ;concat-val (if (and (string? v1) (string? v2))
                  ;             (str v1 " " v2)
                  ;             (if (string? v1)
                  ;               v1
                  ;               (if (string? v2)
                  ;                 v2
                  ;                 "")))
                  concat-val (cond
                               (and (non-empty? v1) (non-empty? v2)) (str v1 " " v2)
                               (and (non-empty? v1) (non-empty? v2)) (str v1)
                               (and (non-empty? v2) (non-empty? v2)) (str v2)
                               :else
                               ""
                               )

                  ]
              (assoc acc k concat-val)))
          (merge m1 m2)
          keys-to-concat))

(defn merge-element [default-container & [args]]
  "合并hccup container央视 class name 为合并 其他为覆盖"
  (let [
        merge-map (reduce conj
                          default-container
                          args)
        maps (filter map? merge-map)
        mm (if (empty? maps)
             {}
             (if (= 1 (count maps))
               (first maps)
               (merge-maps-with-flexible-string-concat (first maps) (last maps) [:class :name])
               )
             )

        comps (filter #(not (map? %)) merge-map)
        ]
    (vec (concat (vector (first comps) mm) (vec (rest comps))))
    )
  )
(defmacro defelem [elements]
  "生成自定义组件宏 根据默认样式 :name 生成对应的函数定义"
  ;(def elements [{:name "li-test" :style [:li {:class "list-group-item"}]}])
  `(do
     ~@(for [elem elements]
         `(defn ~(symbol (:name elem)) [& args#]
            (merge-element ~(:style elem) args#)
            )
         )
     )
  )
